package com.rain.cy.business.service;

import cn.hutool.core.util.ObjectUtil;
import com.rain.cy.business.mapper.ActionCountMapper;
import com.rain.cy.business.model.entity.ActionCountEntity;
import com.rain.cy.framework.entity.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 埋点信息 服务实现类
 * </p>
 *
 * @author yuchen
 * @since 2021-10-07
 */
@Service
public class ActionCountService extends BaseServiceImpl<ActionCountMapper, ActionCountEntity> {


    public void plusScoreMoreNum(Integer userId) {
        plusNumsByType(userId, 1);
    }

    public void plusReceiveFeedbackNum(Integer userId) {
        plusNumsByType(userId, 2);
    }

    public void plusSendFeedbackNum(Integer userId) {
        plusNumsByType(userId, 3);
    }

    public void plusShareNum(Integer userId) {
        plusNumsByType(userId, 4);
    }

    private void plusNumsByType(Integer userId, Integer type) {
        ActionCountEntity actionCountEntity = lambdaQuery().eq(ActionCountEntity::getUserId, userId).oneOpt().orElse(new ActionCountEntity());
        actionCountEntity.setUserId(userId);
        switch (type) {
            case 1:
                actionCountEntity.setScoreMoreNum(ObjectUtil.defaultIfNull(actionCountEntity.getScoreMoreNum(), 0) + 1);
                break;
            case 2:
                actionCountEntity.setReceiveFeedbackNum(ObjectUtil.defaultIfNull(actionCountEntity.getReceiveFeedbackNum(), 0) + 1);
                break;
            case 3:
                actionCountEntity.setSendFeedbackNum(ObjectUtil.defaultIfNull(actionCountEntity.getSendFeedbackNum(), 0) + 1);
                break;
            case 4:
                actionCountEntity.setShareNum(ObjectUtil.defaultIfNull(actionCountEntity.getShareNum(), 0) + 1);
                break;
            default:
        }
        saveOrUpdate(actionCountEntity);
    }
}
